//
//  BaseSegmentedPagerVC.m
//  EPTool
//
//  Created by shiqing guo on 2018/5/9.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "BaseSegmentedPagerVC.h"
#import "BaseTVC.h"
#import "UIView+DDAddition.h"

@interface BaseSegmentedPagerVC ()<MXSegmentedPagerDelegate, MXSegmentedPagerDataSource>

@end

@implementation BaseSegmentedPagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置导航栏
    [self setNavRightItem];
    // 设置产品入口和资讯
    [self setSegment];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 导航栏上右边添加 “+” 号按钮
- (void)setNavRightItem
{
    UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btnAdd setImage:[UIImage imageNamed:@"nav_plus"] forState:UIControlStateNormal];
    [btnAdd addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *itemAdd = [[UIBarButtonItem alloc]initWithCustomView:btnAdd];
    self.navigationItem.rightBarButtonItem = itemAdd;
}

- (void)addAction:(id)sender
{
    if (self.rightNavButtonBlock) {
        self.rightNavButtonBlock();
    }
}

- (void)setSegment {
    
    // 头部
    self.segmentedPager = [[MXSegmentedPager alloc] initWithFrame:CGRectMake(0, 0, self.view.width, UI_CONTENT_HEIGHT)];
    self.segmentedPager.defaultIndex = self.jumpPageIndex;
    // 选择栏控制器属性
    self.segmentedPager.segmentedControl.verticalDividerColor = [UIColor clearColor]; // 中间竖线
    self.segmentedPager.segmentedControl.backgroundColor = [UIColor whiteColor];
    self.segmentedPager.segmentedControl.borderWidth = 2.0; // 边框宽度
    self.segmentedPager.segmentedControl.borderColor = [UIColor grayColor]; // 边框颜色
    self.segmentedPager.segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0); // 间距
    self.segmentedPager.segmentedControl.verticalDividerEnabled = NO;// 可以垂直滚动
    self.segmentedPager.segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleFixed;
    self.segmentedPager.pager.transitionStyle = self.transitionStyle;
    // 底部指示器的宽度是否根据内容
    // --默认状态的字体
    self.segmentedPager.segmentedControl.titleTextAttributes =
    @{NSForegroundColorAttributeName:[EPColor epColor_descMessageGray],
      NSFontAttributeName : [UIFont systemFontOfSize:16]};
    // --选择状态下的字体
    self.segmentedPager.segmentedControl.selectedTitleTextAttributes =
    @{NSForegroundColorAttributeName:[EPColor epColor_Nav],
      NSFontAttributeName:[UIFont boldSystemFontOfSize:16]};
    // --其他基本样式
    self.segmentedPager.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segmentedPager.segmentedControl.selectionIndicatorColor = [EPColor epColor_Blue];
    self.segmentedPager.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
    self.segmentedPager.segmentedControl.selectionIndicatorHeight = 3.f; // 需要底部横条指示器
    self.segmentedPager.segmentedControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.segmentedPager.segmentedControlEdgeInsets = UIEdgeInsetsMake(0, 0, self.placeHeight, 0);
//    self.segmentedPager.backgroundColor = [UIColor whiteColor]; // 补齐10个像素的颜色
    self.segmentedPager.backgroundColor = [UIColor clearColor];
    
    self.segmentedPager.delegate = self;
    self.segmentedPager.dataSource = self;
    [self.view addSubview:self.segmentedPager];
}

#pragma -mark <MXSegmentedPagerDelegate>
- (CGFloat)heightForSegmentedControlInSegmentedPager:(MXSegmentedPager *)segmentedPager
{
    return Cell_H;
}

// 滚动整体的时候调用
- (void)segmentedPager:(MXSegmentedPager *)segmentedPager didScrollWithParallaxHeader:(MXParallaxHeader *)parallaxHeader
{
    // 通过拿到滚动的对应的View
    UIScrollView *scrollView = (UIScrollView *)segmentedPager.subviews[0];
    if (0 == scrollView.contentOffset.y)
    {
//        self.navigationController.navigationBar.hidden = NO;
    }
    else
    {
//        self.navigationController.navigationBar.hidden = YES;
    }
}

#pragma -mark <MXSegmentedPagerDataSource>
- (NSInteger)numberOfPagesInSegmentedPager:(MXSegmentedPager *)segmentedPager
{
    return self.titleList.count;
}

- (NSString *)segmentedPager:(MXSegmentedPager *)segmentedPager titleForSectionAtIndex:(NSInteger)index
{
    // 标签页的标题
    return [self.titleList objectAtIndex:index];
}

- (void)segmentedPager:(MXSegmentedPager *)segmentedPager didSelectViewWithIndex:(NSInteger)index
{
    if ([self respondsToSelector:@selector(didSelectIndex:)]) {
        [self didSelectIndex:index];
    }
    self.index = index;
}

- (UIView *)segmentedPager:(MXSegmentedPager *)segmentedPager viewForPageAtIndex:(NSInteger)index
{
    if (self.vcList.count != self.titleList.count) return nil;
    BaseTVC *tempVC = [self.vcList objectAtIndex:index];
    return tempVC.view;
}

- (void)refreshBadgeImage
{
    if ([self respondsToSelector:@selector(badgeImageAtIndex:)]) {
        NSMutableArray *imageList = [[NSMutableArray alloc] initWithCapacity:self.titleList.count];
        for (int i = 0; i < self.titleList.count; i++) {
            [imageList addObject:[self badgeImageAtIndex:i]];
        }
        self.segmentedPager.segmentedControl.badgeImages = [imageList copy];
    }
}

@end
